package com.umakr.ax.common.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Maps;
import com.umakr.ax.common.model.AppVersion;
import com.umakr.ax.common.mapper.AppVersionMapper;
import com.umakr.ax.core.base.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;

/**
 * <p>
 * app版本信息。 服务实现类
 * </p>
 *
 * @author Umakr-Code-Generator
 * @since 2018-03-17
 */
@Service
public class AppVersionService extends BaseService<AppVersionMapper, AppVersion>{

    public Object checkAppVersion(String os,Integer versionCode,String versionName){
        AppVersion appVersionResult = null;
        if(versionCode != null){
            AppVersion appVersion = new AppVersion();
            appVersion.setVersionCode(versionCode);
            appVersion.setVersionType("ios".equals(os) ? 0 : 1);
            AppVersion appVersionSelected =  mapper.selectOne(appVersion);
            if(appVersionSelected != null){
                EntityWrapper<AppVersion> appVersionEntityWrapper = new EntityWrapper<>();
                appVersionEntityWrapper.eq("version_type","ios".equals(os) ? 0 : 1);
                appVersionEntityWrapper.gt("version_code",versionCode);
                appVersionEntityWrapper.orderBy("version_code",true);
                List<AppVersion> appVersionList = mapper.selectList(appVersionEntityWrapper);
                if(!appVersionList.isEmpty()){
                    appVersionResult = appVersionList.get(appVersionList.size() -1);
                }
            }
        }
        if(StringUtils.isNotEmpty(versionName)){
            AppVersion appVersion = new AppVersion();
            appVersion.setVersionName(versionName);
            appVersion.setVersionType("ios".equals(os) ? 0 : 1);
            AppVersion appVersionSelected =  mapper.selectOne(appVersion);
            if(appVersionSelected != null){
                EntityWrapper<AppVersion> appVersionEntityWrapper = new EntityWrapper<>();
                appVersionEntityWrapper.eq("version_type","ios".equals(os) ? 0 : 1);
                appVersionEntityWrapper.gt("stime",appVersionSelected.getStime());
                appVersionEntityWrapper.orderBy("stime",true);
                List<AppVersion> appVersionList = mapper.selectList(appVersionEntityWrapper);
                if(!appVersionList.isEmpty()){
                    appVersionResult = appVersionList.get(appVersionList.size() -1);
                }
            }
        }

        Map<String,Object> resultMapData = Maps.newHashMap();
        resultMapData.put("errorCode",appVersionResult == null ? 0 : 1);
        resultMapData.put("msg",appVersionResult == null?"无新版本。":"有新版本。");
        if(appVersionResult != null){
            Map<String,Object> resultData = Maps.newHashMap();
            resultMapData.put("data",resultData);
            resultData.put("content",appVersionResult.getContent());
            resultData.put("newVersionCode",appVersionResult.getVersionCode());
            resultData.put("newVersionName",appVersionResult.getVersionName());
            resultData.put("newPackageName",appVersionResult.getPackageName());
        }
        return resultMapData;
    }

    public Object getNewAppVersionName() {
        List<AppVersion> appVersionList =  baseMapper.getNewAppVersionName();
        Map<String,Object> resultMapData = Maps.newHashMap();
        resultMapData.put("errorCode",1);
        resultMapData.put("msg","");
        resultMapData.put("data",appVersionList.get(0));
        return resultMapData;
     }

}
